package com.example.kafkatest.service;

import com.example.kafkatest.config.KafkaConsts;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Slf4j
@Component
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SaticScheduleTask {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    //3.添加定时任务
    @Scheduled(cron = "0/5 * * * * ?")
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    private void configureTasks() {

        StringBuffer str = new StringBuffer();
        str.append(UUID.randomUUID().toString().replaceAll("-",""))
              .append("116.29687775585936").append("40.02913594009122");

        log.info("发送消息: {}", str.toString());
        kafkaTemplate.send(KafkaConsts.TOPIC_TEST,str.toString());
    }

}
